﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.Linq;
using System.Threading;
using JTG.ServiceInterface.Factory;
using JTG.ServiceInterface.InterfaceService;
using Tzw.Common;
using Tzw.Common.Model;

namespace JTG.CheckInMachine.Ybfby
{
    public partial class ReadCard : BaseForm
    {
        private IPrinterService _printerService;
        private List<PrinterInfo> _printerInfos;
        private PrinterHelper _printer = new PrinterHelper();
        private static string _funcName = ConfigurationManager.AppSettings["ConfigReflectionFunc"];

        private int _timeOutClose = App.GlobSettings.OperateShowTimes;
        private int _w;
        private int _h;
        public ReadCard(int w, int h) : base(w, h)
        {
            _w = w;
            _h = h;
            InitializeComponent();
        }

        private void Main_Load(object sender, System.EventArgs e)
        {
            //关闭按钮事件
            CloseEvent += (s, e1) =>
            {
                pwdKeyboard1.CloseTimeReset();
                pwdKeyboard1.Visible = true;
            };

            pwdKeyboard1.CloseEvent += (o, args) => { Close(); };

            textBox1.GotFocus += TextBox1_GotFocus;
            keyboard.NumberEvent += NumberEvent;
            keyboard.OkEvent += OkEvent;
            keyboard.CleanEvent += CleanEvent;
            keyboard.DeleteEvent += DeleteEvent;

            base_lab_time.Visible = false; //隐藏时间lab

            this.BackgroundImage = Image.FromFile("Imgs\\1024.768\\宜宾市妇幼保健院2.jpg");

            //文本输入框
            textBox1.Left = 231;
            textBox1.Top = 153;
            textBox1.Width = 499;
            textBox1.Height = 40;

            //显示键盘的按钮
            btn_KeyBoard.Left = 749;
            btn_KeyBoard.Top = 146;

            //键盘
            keyboard.Width = 408;
            keyboard.Height = 368;
            keyboard.Left = 276;
            keyboard.Top = 232;

            //扫条码gif动画
            lab_sacnqrcode.Width = 419;
            lab_sacnqrcode.Height = 425;
            lab_sacnqrcode.Left = 272;
            lab_sacnqrcode.Top = 200;

            //等待动画
            lab_loading.Left = 436;
            lab_loading.Top = 330;

            //加载打印机检查服务
            _printerService = new PrintServiceFactory().CreatePrintService();
        }

        #region 键盘事件

        private void NumberEvent(object sender, EventArgs e)
        {
            textBox1.Text += sender.ToString();
            _timeOutClose = App.GlobSettings.OperateShowTimes;
        }

        private void OkEvent(object sender, EventArgs e)
        {
            btn_ok_Click(sender, e);
            _timeOutClose = App.GlobSettings.OperateShowTimes;
        }

        private void DeleteEvent(object sender, EventArgs e)
        {
            if (textBox1.Text.Length > 0)
                textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
            _timeOutClose = App.GlobSettings.OperateShowTimes;
        }

        private void CleanEvent(object sender, EventArgs e)
        {
            textBox1.Text = "";
            _timeOutClose = App.GlobSettings.OperateShowTimes;
        }

        #endregion

        private void btn_KeyBoard_Click(object sender, System.EventArgs e)
        {
            keyboard.Visible = !keyboard.Visible;
            lab_sacnqrcode.Visible = !keyboard.Visible;
            textBox1.Focus();
        }

        private void btn_ok_Click(object sender, System.EventArgs e)
        {
            //数据交互，界面等待中
            DataTrans(true);


            //异步对接lis
            new Thread(() =>
            {
                try
                {
                    //加载打印机信息
                    _printerInfos = _printerService.GetList();

                    var printStatus = Enums.ReportStatus.Printing;
                    var printName = "";

                    if (_printerInfos == null || !_printerInfos.Any())
                    {
                        printStatus = Enums.ReportStatus.Error;
                    }
                    else
                    {
                        //对接lis信息
                        //TODO
                        //查询病人信息
                        //var ins = new HisBridgeFactory().CreatePrintService(_funcName);
                        //ins.Request("", "", "");

                        //是否需要打印
                        if (Enums.ReportStatus.Printing == printStatus || Enums.ReportStatus.PrintingAndHasReport == printStatus)
                            printName = _printer.ChangePrinter(_printerInfos); //设置默认打印机
                    }


                    //界面展示完后恢复界面
                    Invoke(new Action(() =>
                    {
                        try
                        {
                            //展示窗口
                            new ShowInfo(printStatus, _w, _h, _printerService, printName).ShowDialog();
                        }
                        catch (Exception exception)
                        {
                            Console.WriteLine(exception);
                            throw;
                        }

                        textBox1.Text = "";
                        DataTrans(false);
                        textBox1.Focus();
                    }));

                }
                catch (Exception ex)
                {
                    Invoke(new Action(() =>
                    {
                        textBox1.Text = "";
                        DataTrans(false);
                        textBox1.Focus();
                    }));
                    Logger.Default.Error($"lis对接异常，异常信息：{ex.Message},{ex.StackTrace}", ex);
                }
            }).Start();

            //lis数据返回关闭等待窗口

        }

        private void TextBox1_GotFocus(object sender, EventArgs e)
        {
            textBox1.Select(0, textBox1.Text.Length);
        }

        private void Main_Activated(object sender, EventArgs e)
        {
            textBox1.Focus();
        }


        public void DataTrans(bool hide)
        {
            //隐藏键盘，动画；显示等待；输入框不可用，按钮不可用
            if (hide)
            {
                lab_sacnqrcode.Visible = false;
                keyboard.Visible = false;
                textBox1.Enabled = false;
                btn_KeyBoard.Enabled = false;

                lab_loading.Visible = true;
            }
            else
            {
                lab_sacnqrcode.Visible = true;
                keyboard.Visible = false;
                textBox1.Enabled = true;
                btn_KeyBoard.Enabled = true;

                lab_loading.Visible = false;
            }

        }
    }
}